package com.learn.blog.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.learn.blog.admin.config.SecurityConfig;
import com.learn.blog.admin.dao.mapper.AdminMapper;
import com.learn.blog.admin.dao.mapper.AdminPermissionMapper;
import com.learn.blog.admin.dao.mapper.PermissionMapper;
import com.learn.blog.admin.model.params.PageParam;
import com.learn.blog.admin.pojo.Admin;
import com.learn.blog.admin.pojo.Admin_permission;
import com.learn.blog.admin.pojo.Permission;
import com.learn.blog.admin.vo.PageResult;
import com.learn.blog.admin.vo.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Package: com.learn.blog.admin.service
 * @ClassName: PermissionService
 * @Author: 陈威
 * @CreateTime: 2021/9/14 17:27
 * @Description:
 */
@SuppressWarnings("all")
@Service
@Transactional
public class PermissionService {

    @Autowired
    PermissionMapper permissionMapper;

    @Autowired
    AdminMapper adminMapper;
    @Autowired
    AdminPermissionMapper adminPermissionMapper;

    public Result listPermission(PageParam pageParam) {
        Page<Permission> page = new Page<>(pageParam.getCurrentPage(), pageParam.getPageSize());
        LambdaQueryWrapper<Permission> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNoneBlank(pageParam.getQueryString())) {
            queryWrapper.like(Permission::getName, pageParam.getQueryString());
        }
        Page<Permission> selectPage = permissionMapper.selectPage(page, queryWrapper);
        PageResult<Permission> pageResult = new PageResult<>();
        pageResult.setList(selectPage.getRecords());
        pageResult.setTotal(selectPage.getTotal());
        return Result.success(pageResult);
    }

    public Result add(Permission permission) {
        this.permissionMapper.insert(permission);
        return Result.success(null);
    }

    public Result update(Permission permission) {
        this.permissionMapper.updateById(permission);
        return Result.success(null);
    }

    public Result delete(Long id) {
        this.permissionMapper.deleteById(id);
        return Result.success(null);
    }

    //添加用户
    public Result addUser(Admin admin) {
        String password = admin.getPassword();
        String newPwd = SecurityConfig.jiaMi(password);
        admin.setPassword(newPwd);
        adminMapper.insert(admin);
        Long permissionId=1L;
        Admin_permission admin_permission = new Admin_permission();
        admin_permission.setId(admin.getId());
        admin_permission.setAdminId(admin.getId());
        admin_permission.setPermissionId(1L);
        adminPermissionMapper.insert(admin_permission);
        return Result.success(null);
    }
    //修改用户
    public Result updateUser(Admin admin) {
        adminMapper.update(admin,new LambdaQueryWrapper<Admin>());
        return Result.success(null);
    }
    //删除用户
    public Result deleteUser(Long id) {
        adminMapper.deleteById(id);
        return Result.success(null);
    }


}